/*
 * @Autor: 周萌钊
 * @Date: 2024-04-01 16:02:49
 * @LastEditors: lh
 * @LastEditTime: 2024-05-17 16:44:31
 * @Description: 接口AES加密
 * 
 */
import CryptoJS from 'crypto-js/crypto-js';
import  {md5} from 'js-md5';

// 偏移量
const iv = CryptoJS.enc.Utf8.parse('crapX(Ue^RDbaUNw');
export const randomNum = '123456789';//随机ID
/**
 * 创建AES加密的Key，加密解密的内容的key
 * @returns newKey 秘钥
 * 前端入参加密key规则：md5加密(用户id(不足补位数字:0)使用截取前4位+token最后一部分截取前8位)生成md5转小写, 截取16位 不足补位字母：x
 * 前端加密(后端解密)规则：随机ID(前端来源放请求头部randomNum;后端来源放响应数据里面randomNum) + AES Key 生成md5转小写 截取16位 不足补位字母：x
 */
export function createKey(token: string) {
  const key = token;
  const newKey = md5(key);
  const lowerKey = newKey.toLowerCase().slice(0,16)
  // console.log('lowerKey=========', lowerKey, lowerKey.length);
  return lowerKey;
}
/**
 * AES解密
 * @param word 需要解密的参数
 */
export function Decrypt(word: any, keyVal: string = '') {
  // const lowerKey = localStorage.getItem('aesKey');
  const lowerKey = createKey(keyVal+ 'hyTkMVj#itTAebN6') 
  // const lowerKey = createKey(keyVal+ 'yD(EHrfj2hmLmCOf') 
  const key = CryptoJS.enc.Utf8.parse(lowerKey);
  // const key = 'jad8fntT1N0r6vyY'
  let decrypted = CryptoJS.AES.decrypt(word, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return decrypted.toString(CryptoJS.enc.Utf8);
}
/**
 * AES加密，内容加密
 * @param word 需要加密的参数
 */
export function Encrypt(word: any,keyVal:string='') {
  // const lowerKey = localStorage.getItem('aesKey');
  const lowerKey = createKey(keyVal+'yD(EHrfj2hmLmCOf')
  const key = CryptoJS.enc.Utf8.parse(lowerKey);
  // debugger;
  // const key = 'jad8fntT1N0r6vyY'
  const srcs = CryptoJS.enc.Utf8.parse(word);
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString();
}

/**
 * 加密 key
 * 
*/
export function encryption_key(userid='0000',token='xxxxxxxx'){
  // console.log('e_KEY---',e_KEY);
  
  function forLength(vals='',len=0,k=''){
    if( vals.length < len ){
      let newvals = '';
      let lens = len - vals.length;
      newvals = vals
      for(let i=0;i<lens;i++){
        newvals += k
      }
      
      return newvals
    }else{
      return vals
    }
  } 
  let uids = forLength(userid,4,'0')
  let loca_token = forLength(token,8,'x').split('.')
  let tokenss = loca_token[loca_token.length-1]
  // let uids = '';
  // if( userid.length < 4  ){
  //   let len = 4 - userid.length;
  //   // uids = userid
  //   for(let i=0;i<len;i++){
  //     uids += '0'
  //   }
  //   uids += userid
  // }
  // let tokenss = '';
  // if( token.length < 8  ){
  //   let len = 4 - token.length;
  //   for(let i=0;i<len;i++){
  //     uids += 'x'
  //   }
  //   tokenss += token
  // }
  console.log(uids.substring(0,4)+tokenss.substring(0,8),'-----tokenss-------');
  
  let newkeys = md5(uids.substring(0,4)+tokenss.substring(0,8)).toLowerCase();
  // console.log(newkeys,newkeys.length,'---newkeys---');
  return newkeys.substring(0,16)

}